<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../css/common.css">
    <link rel="stylesheet" href="../css/main.css">
    <link rel="stylesheet" href="../css/jquery-ui.css">
    <link href="../css/bootstrap.css" rel="stylesheet" type="text/css" />
    <title>Document</title>
    <style>
    </style>
</head>
<body>
<div class="box">
    <div class="box_border" style="border-bottom: none;">
        <div class="box_top">
            <b class="pl15" >添加人员:<span id="sectionName"></span></b>
            <a href='sectionmanager_edit.html'><img class="mt10 mr10" src="../images/ext_btn_del.png" style="float: right;"></a>
        </div>
        <div class="box_center mt10" style="padding-left: 5%;">
            <table class="form_table" border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td><input type="text" id="searchText" name="search" class="input-text lh30" size="40"></td>
                    <td><input type="button" value="搜索" class="ext_btn ext_btn_submit" onclick="searchUserByName()"></td>
                </tr>
            </table>
        </div>
        <div style="clear: both;"></div>
        <div class="">
            <div id="table" class="">
                <div class="box span9 oh" style="margin-left: auto; margin-right: auto">
                    <table id="approval_info1" width="100%" border="0" cellpadding="0" cellspacing="0" class="list_table" style="table-layout:fixed; font-size:1.2rem">
                        <tr>
                            <th class="td_center" style="width:5%;text-align: center;"><input type="checkbox" name="checkAll"  onclick="checkAll()" ></th>
                            <th class="td_center" >账号</th>
                            <th class="td_center" >姓名</th>
                            <th class="td_center" >可选操作</th>
                        </tr>
                        <tbody id="approval_info">
                        </tbody>
                    </table>
                    <div class="page mt10 fl">
                    <a class="ext_btn add" onclick="batchAddUser()"><span class="add"  ></span>批量添加到部门</a>
                        </div>
                    <div class="page mt10 fr" style="margin-top: -10px;">
                        <div class="pagination">
                            <ul id="ul">
                                <li class="first-child " id="li1"><a onclick="firstLastPage(0)">首页</a></li>
                                <li class="" id="li2"><a onclick="upDownPage(-1)">上一页</a></li>
                                <li class="" id="li3"><a onclick="upDownPage(1)">下一页</a></li>
                                <li class="last-child " id="li4"><a onclick="firstLastPage(1)">末页</a></li>
                            </ul>
                        </div>
                        </div>
                    <div class="page mt10">
                        <div class="pagination" id="list_page_div">
                            <ul id="list_page">
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="addToSection" style="display: none;">
    <form name="formToSection">
        部门职位:<input type="text" id="addToSectionText" name="search" class="input-text lh30" size="25">
    </form>
</div>
<div id="info" style="display: none;text-align: center;"></div>
</body>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui.js"></script>
<script type="text/javascript" src="../js/common.js"></script>
<script type="text/javascript" src="../js/config.js"></script>
<script type="text/javascript" src="../js/config.js"></script>

<script type="text/javascript">

    function chakan(e){
        var tr=e.parentNode.parentNode;
        var user_ID=tr.cells[1].innerHTML;
        if(confirm("是否添加到部门？")){
            var section=JSON.parse(window.localStorage.getItem('all_sectionuser'));
          //  alert(section);
            var section_ID=section['section']['section_ID'];
           // var section_ID={section_ID:section_ID};
           // alert(section_ID);
            var section_add={user_ID:user_ID,section_ID:section_ID,section_admin:0};
            $.ajax({
                url:url + "Sectionmanager_add_user.php",
                type: "POST",
                data:section_add,
                success:function(data){
                    //alert(data);
                   if(data==1)
                   alert("添加成功！");
                    else
                   alert("添加失败！");
                    //window.localStorage.setItem('all_sectionuser', data);
                }});
        }
    }
    //加载数据
    function nextPage(){
        var phpUrl=url+'admin/Section/SectionGetAddUserPageData.php';
        $.ajax({
            url:phpUrl,
            type:"POST",
            data:{model:"getUserDataUsePage",curPage:curPage,sectionId:sectionId},
            success: function (userData) {
//                alert(userData);
                userData = JSON.parse(userData);
                if (userData['state'] == 1) {
                     var tableData=userData['data'];
//                    alert(tableData.length);
                      createTable(tableData);
                }
            }
    });
    }
    //数据加载表格
    function createTable(tableData){
        $("#approval_info").empty();
        for (var i = 0; i < tableData.length; i++) {
            $("#approval_info").append(
                    '<tr><td style="text-align: center;"><input type="checkbox" name="userCheck"  onclick="changeCheckAll()" value="'+tableData[i]['user_ID']+'"></td></td>' +
                    '<td class="td_center  " >' + tableData[i]['user_number'] + '</td>' +
                    '<td class="td_center  " >' + tableData[i]['user_name'] + '</td>' +
                    '<td class="td_center"><a class="ext_btn add" onclick="addToSection(' + tableData[i]['user_ID'] + ')"><span class="add" ></span>添加到部门</a></td></tr>'
            );
        }
    }
    //获取人员数量
    function getUserCount(){
        var phpUrl=url+'admin/Section/SectionGetAddUserCount.php';
        $.ajax({
           url:phpUrl,
           type:"POST",
           async:false,
           data:{model:'getUserCount',sectionId:sectionId},
           success: function (userData) {
//               alert(userData);
                userData=JSON.parse(userData);
                if(userData['state']==1){
//                    alert(userData['data']);
                     pageCounts=Math.ceil(userData['data']/10);
//                    alert(pageCounts);
                }
            }
        });
    }
    //首尾跳页
    function firstLastPage(e){
        $("#ul").show();
        if(e==0) {
            curPage=0;
            $("#ul li").addClass("disabled");
            $("#li3").removeClass("disabled");
            $("#li4").removeClass("disabled");
        }
        else{
//      alert(pageCounts);
            curPage=pageCounts-1;
            $("#ul li").addClass("disabled");
            $("#li1").removeClass("disabled");
            $("#li2").removeClass("disabled");
        }
        if(pageCounts==1) $("#ul li").addClass("disabled");
        nextPage();
    }
    //上下翻页
    function upDownPage(e){
        $("#ul").show();
        if(curPage>=0||curpage<=pageCounts-1){
            if(e<0){
                if(curPage>0)  {
                    curPage--;
                    if(curPage>0) $("#ul li").removeClass("disabled");
                    else{
                        $("#ul li").removeClass("disabled");
                        $("#li1").addClass("disabled");
                        $("#li2").addClass("disabled");
                    }
                }
            }else{
                if(curPage<pageCounts-1) {
                    curPage++;
                    if(curPage<pageCounts-1) $("#ul li").removeClass("disabled");
                    else{
                        $("#ul li").removeClass("disabled");
                        $("#li3").addClass("disabled");
                        $("#li4").addClass("disabled");
                    }
                }
            }
            if(pageCounts==1) $("#ul li").addClass("disabled");
            nextPage();
        }
    }
    //搜索
    function searchUserByName(){
//        alert(1);
        var phpUrl=url+'admin/Section/SectionGetAddSearchUserData.php';
//        alert($("#searchText").val());
        $.ajax({
            url:phpUrl,
            type:"POST",
            data:{model:"searchUserDataByName",user_name:$("#searchText").val(),section_ID:sectionId},
            success: function (userData) {
//                alert(userData);
                userData = JSON.parse(userData);
                if (userData['state'] == 1) {
                    var tableData=userData['data'];
//                    alert(tableData.length);
                      createTable(tableData);
                }
            }
        });
    }
    //添加到部门
    function addToSection(e){
        var phpUrl=url+'admin/Section/SectionInsertAddToSection.php';
        $("#addToSection").dialog({
            resizable: false,
            title: "信息",
            modal: true,
            buttons: {
                '取消': function () {
                    $("#addToSection").dialog("close");
                },
                '确定': function () {
                    if ($("#addToSectionText").val() != '') {
                        $("#addToSection").dialog("close");
                        $.ajax({
                            url: phpUrl,
                            type: "POST",
                            data: {
                                model: 'addToSection',
                                user_ID: e,
                                section_ID: sectionId,
                                section_job: $("#addToSectionText").val()
                            },
                            success: function (state) {
                                state = JSON.parse(state);
                                if (state['state'] == 1)
                                    document.getElementById('info').innerHTML = '添加成功!';
                                else
                                    document.getElementById('info').innerHTML = '添加失败！';
                                $("#info").dialog({
                                    resizable: false,
                                    modal: true,
                                    buttons: {
                                        '确定': function () {
                                            $("#info").dialog('close');
                                        }
                                    }
                                });
                            }
                        });
                    }else {
                        document.getElementById('info').innerHTML = '内容不能为空！';
                        $("#info").dialog({
                            resizable: false,
                            modal: true,
                            buttons: {
                                '确定': function () {
                                    $("#info").dialog('close');
                                }
                            }
                        });
                    }
                }
            }
        });
    }
    //全选
    function checkAll(){
        var check=$('input[name="userCheck"]');
        if($('input[name="checkAll"]')[0].checked)
            for(var i=0;i<check.length;i++)
                check[i].checked=true;
        else
            for(var i=0;i<check.length;i++)
                check[i].checked=false;
    }
    //批量添加
    function batchAddUser(){
        $('input[name="checkAll"]')[0].checked=false;//全选去掉
        var phpUrl=url+'admin/Section/SectionInsertAddToSection.php';
        var check = $('input[name="userCheck"]');
        var flag=false;
        for (var i = 0; i < check.length; i++)
            if(check[i].checked){
                flag=true;
                break;
            }
        if(flag)
        $("#addToSection").dialog({
            buttons: {
                '取消': function () {
                    $("#addToSection").dialog("close");
                },
                '确定': function () {
                    var sectionJob = $("#addToSectionText").val();
                    if (sectionJob != '') {
                        $("#addToSection").dialog("close");
                        for (var i = 0; i < check.length; i++)
                        if(check[i].checked)
                            $.ajax({
                                url: phpUrl,
                                type: "POST",
                                data: {
                                    model: 'addToSection',
                                    user_ID: check[i].value,
                                    section_ID: sectionId,
                                    section_job: sectionJob
                                },
                                success: function (state) {
                                    state = JSON.parse(state);
                                    if (state['state'] == 1)
                                        document.getElementById('info').innerHTML = '添加成功!';
                                    else
                                        document.getElementById('info').innerHTML = '添加失败！';
                                    $("#info").dialog({
                                        resizable: false,
                                        modal: true,
                                        buttons: {
                                            '确定': function () {
                                                $("#info").dialog('close');
                                            }
                                        }
                                    });
                                }
                            });
                    }else{
                        document.getElementById('info').innerHTML = '职位不能为空！';
                        $("#info").dialog({
                            resizable: false,
                            modal: true,
                            buttons: {
                                '确定': function () {
                                    $("#info").dialog('close');
                                }
                            }
                        });
                    }
                }
            }
        });
    }
    //获取基本部门信息
    function getSectionName(){
//        alert(1);
        var phpUrl=url+'admin/Section/SectionGetSectionData.php';
        $.ajax({
            url:phpUrl,
            type:"POST",
            data:{model:'getSectionData',section_ID:sectionId},
            success: function (sectionData) {
//                alert(sectionData);
                sectionData=JSON.parse(sectionData);
                if(sectionData['state']=-1) {
                    var sectionData=sectionData['data'][0];
//                    alert(sectionData['section_name']);
                    $("#sectionName").html(sectionData['section_name']);
//                    sectionId=sectionData['section_ID'];
                }
            }
        })
    }
    //分页操作
    var curPage=0;
    var pageCounts=null;
    var sectionId;
    //网页初始化
    $(function () {
        var localSectionIdData=JSON.parse(window.localStorage.getItem('editSectionId'));
        sectionId=localSectionIdData['sectionId'];
//        alert('部门ID：'+sectionId);
          getUserCount();
        firstLastPage(0);
        getSectionName();
//        alert(pageCounts);
    })
</script>
</html>
  